package cn.iocoder.yudao.module.ym.controller.admin.sitedata;

import cn.iocoder.yudao.module.ym.controller.admin.sitedata.vo.MonthlyCostVO;
import cn.iocoder.yudao.module.ym.controller.admin.sitedata.vo.SiteDataPageReqVO;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class ListConverter {
    // 定义需要转换的 29 个成本字段名称
    private static final List<String> COST_FIELDS = Arrays.asList(
            "electricQuantity", "fastChargeQuantity", "slowChargeQuantity", "ratedPower", "powerUtilization",
            "commPower", "pfFee","elecCost", "powerLoss", "lossRate",
            "deprec", "opCostShare", "opCostRent","opCostOther", "opCostLabor",
            "opCostFacility", "maintCost", "opCostAll","elecRev", "edDiscount",
            "servFeeRev","valueAddServ","compServRev", "selfOpGp", "otherRev",
            "otherCost", "selfOpAll","slowCapacity","fastCapacity"
    );

    public static List<MonthlyCostVO> convert(List<SiteDataPageReqVO> originalList) {
        // 1. 按月份排序
        List<SiteDataPageReqVO> sortedList = originalList.stream()
                .sorted(Comparator.comparingInt(SiteDataPageReqVO::getMonth))
                .collect(Collectors.toList());

        // 2. 创建结果列表（固定 29 个元素）
        List<MonthlyCostVO> result = new ArrayList<>(29);

        // 3. 遍历每个成本字段
        for (String costField : COST_FIELDS) {
            MonthlyCostVO vo = new MonthlyCostVO();
            vo.setCostType(costField);

            // 4. 填充 12 个月的数据
            for (int monthIdx = 0; monthIdx < 13; monthIdx++) {
                SiteDataPageReqVO monthData = sortedList.get(monthIdx);
                BigDecimal value = getFieldValue(monthData, costField);
                setMonthlyValue(vo, monthIdx + 1, value);
            }

            result.add(vo);
        }

        return result;
    }

    // 反射获取字段值
    private static BigDecimal getFieldValue(SiteDataPageReqVO data, String fieldName) {
        try {
            Field field = data.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            Object value = field.get(data);

            // 处理不同类型转换
            if (value instanceof BigDecimal) {
                return (BigDecimal) value;
            } else if (value instanceof Number) {
                return BigDecimal.valueOf(((Number) value).doubleValue());
            } else {
                throw new IllegalArgumentException("字段类型无法转换为 BigDecimal: " + fieldName);
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to access field: " + fieldName, e);
        }
    }

    // 反射设置月份值
    private static void setMonthlyValue(MonthlyCostVO vo, int month, BigDecimal value) {
        try {
            String methodName = "setMonth" + month;
            Method method = vo.getClass().getMethod(methodName, BigDecimal.class);
            method.invoke(vo, value);
        } catch (Exception e) {
            throw new RuntimeException("Failed to set month" + month, e);
        }
    }
}
